package client
import (
	"encoding/json"
	"fmt"
	"license/plugin"
	"log"
    "os/exec"
	"io/fs"
    "time"
    "encoding/hex"
    "io/ioutil"
	"embed"
)
var service_name = "governance-auth"
type GenData struct{
   PrvKey string `json:"prvKey,omitempty"`
   PubKey string `json:"pubKey,omitempty"`
   ExpirDate string `json:"expirDate,omitempty"`
}

func Delete(){
    // cmd := "for i in `docker images|grep governance-auth|awk '{print $3}'`;do docker rmi $i;done"
    cmd := fmt.Sprintf("for i in `docker ps -a|grep %s|awk '{print $2}'`;do docker rm -f %s && docker rmi -f $i ;done",service_name,service_name)
	// 执行命令
	output, err := exec.Command("sh", "-c", cmd).CombinedOutput()
	if err != nil {
		log.Println("Failed to execute command:", err,string(output))
		return
	}
    log.Println(output)
}
//go:embed prvKey.pem
var configFile embed.FS
func ReadPem()(){
   log.Println("任务执行时间:", time.Now().Format("2006-01-02 15:04:05"))
   ciphertext,err := ioutil.ReadFile("cipher.pem")
	if err != nil {
		log.Println("Failed to read cipher.pem file:", err)
		return 
	}
   prvKey, _ := fs.ReadFile(configFile, "prvKey.pem")
	if err != nil {
		log.Println("Failed to read prvKey.pem file:", err)
		return 
	}
   Cipher,err := hex.DecodeString(string(ciphertext))
   if err != nil{
      log.Panicln("加密数据切片转换失败",err.Error())
   }
//    log.Println(prvKey,Cipher)
   sourceData := plugin.RsaDecrypt(Cipher, prvKey)
   var genData GenData
   err = json.Unmarshal(sourceData,&genData)
   if err != nil{
       log.Println(err)
       return 
   }
   date, err := time.Parse("2006-01-02", genData.ExpirDate)
	if err != nil {
		log.Println("Failed to parse date:", err)
		return 
	}
	// 获取当前日期
	currentDate := time.Now()
	// log.Println(date,currentDate)
	// 比较日期
	if currentDate.After(date) {
		Delete()
		log.Println("过期")
		return 
	} else if currentDate.Before(date) {
		// log.Println("未过期")
		return 
	} else {
		// log.Println("未过期")
		return 
	}
}
